//-------------------------------------------------------------------------------------------------
// <copyright file="Tool_Options.cs" company="Microsoft">
//    Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
// 
// <summary>
// Form to set the global tool options for DUAScriptGen.
// </summary>
//-------------------------------------------------------------------------------------------------

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Security.Permissions;

[assembly: RegistryPermissionAttribute(SecurityAction.RequestMinimum,ViewAndModify = "HKEY_CURRENT_USER")]

namespace DUAScriptGen
{
	/// <summary>
	/// Summary description for Tool_Options.
	/// </summary>
	public class Tool_Options : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.RadioButton btnHttpPolling;
		private System.Windows.Forms.RadioButton btnSharePolling;
		private System.Windows.Forms.TextBox txtLocalDUADrive;
		private System.Windows.Forms.TextBox txtLocationToPoll;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label labelLocationHint;
		private System.Windows.Forms.Button btnOK;
		private System.Windows.Forms.Button btnCancel;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.CheckBox chkRebootAfterScript;
		private System.Windows.Forms.TextBox txtNextScriptName;
		private System.Windows.Forms.Label label2;

		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Tool_Options()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.btnOK = new System.Windows.Forms.Button();
			this.btnCancel = new System.Windows.Forms.Button();
			this.label3 = new System.Windows.Forms.Label();
			this.txtLocalDUADrive = new System.Windows.Forms.TextBox();
			this.txtLocationToPoll = new System.Windows.Forms.TextBox();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.labelLocationHint = new System.Windows.Forms.Label();
			this.label1 = new System.Windows.Forms.Label();
			this.btnSharePolling = new System.Windows.Forms.RadioButton();
			this.btnHttpPolling = new System.Windows.Forms.RadioButton();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.label2 = new System.Windows.Forms.Label();
			this.txtNextScriptName = new System.Windows.Forms.TextBox();
			this.chkRebootAfterScript = new System.Windows.Forms.CheckBox();
			this.groupBox1.SuspendLayout();
			this.groupBox2.SuspendLayout();
			this.SuspendLayout();
			// 
			// btnOK
			// 
			this.btnOK.Location = new System.Drawing.Point(240, 328);
			this.btnOK.Name = "btnOK";
			this.btnOK.TabIndex = 0;
			this.btnOK.Text = "OK";
			this.btnOK.Click += new System.EventHandler(this.OK_Click);
			// 
			// btnCancel
			// 
			this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.btnCancel.Location = new System.Drawing.Point(328, 328);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.TabIndex = 1;
			this.btnCancel.Text = "Cancel";
			this.btnCancel.Click += new System.EventHandler(this.Cancel_Click);
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(16, 35);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(96, 14);
			this.label3.TabIndex = 9;
			this.label3.Text = "Local DUA folder";
			// 
			// txtLocalDUADrive
			// 
			this.txtLocalDUADrive.Location = new System.Drawing.Point(128, 32);
			this.txtLocalDUADrive.Name = "txtLocalDUADrive";
			this.txtLocalDUADrive.Size = new System.Drawing.Size(256, 20);
			this.txtLocalDUADrive.TabIndex = 10;
			this.txtLocalDUADrive.Text = "";
			// 
			// txtLocationToPoll
			// 
			this.txtLocationToPoll.Location = new System.Drawing.Point(128, 88);
			this.txtLocationToPoll.Name = "txtLocationToPoll";
			this.txtLocationToPoll.Size = new System.Drawing.Size(256, 20);
			this.txtLocationToPoll.TabIndex = 6;
			this.txtLocationToPoll.Text = "";
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.labelLocationHint);
			this.groupBox1.Controls.Add(this.label1);
			this.groupBox1.Controls.Add(this.btnSharePolling);
			this.groupBox1.Controls.Add(this.btnHttpPolling);
			this.groupBox1.Controls.Add(this.txtLocationToPoll);
			this.groupBox1.Location = new System.Drawing.Point(11, 8);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(397, 168);
			this.groupBox1.TabIndex = 2;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Polling Options";
			// 
			// labelLocationHint
			// 
			this.labelLocationHint.Location = new System.Drawing.Point(16, 120);
			this.labelLocationHint.Name = "labelLocationHint";
			this.labelLocationHint.Size = new System.Drawing.Size(368, 32);
			this.labelLocationHint.TabIndex = 7;
			this.labelLocationHint.Text = "The location should be the name of a valid HTTP server and an optional relative p" +
				"ath on that server";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(16, 90);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(88, 16);
			this.label1.TabIndex = 5;
			this.label1.Text = "Location to poll";
			// 
			// btnSharePolling
			// 
			this.btnSharePolling.Location = new System.Drawing.Point(16, 56);
			this.btnSharePolling.Name = "btnSharePolling";
			this.btnSharePolling.Size = new System.Drawing.Size(120, 16);
			this.btnSharePolling.TabIndex = 4;
			this.btnSharePolling.Text = "Poll share location";
			this.btnSharePolling.CheckedChanged += new System.EventHandler(this.btnSharePolling_CheckedChanged);
			// 
			// btnHttpPolling
			// 
			this.btnHttpPolling.Location = new System.Drawing.Point(16, 32);
			this.btnHttpPolling.Name = "btnHttpPolling";
			this.btnHttpPolling.Size = new System.Drawing.Size(120, 16);
			this.btnHttpPolling.TabIndex = 3;
			this.btnHttpPolling.Text = "Poll HTTP location";
			this.btnHttpPolling.CheckedChanged += new System.EventHandler(this.btnHttpPolling_CheckedChanged);
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.Add(this.label2);
			this.groupBox2.Controls.Add(this.txtNextScriptName);
			this.groupBox2.Controls.Add(this.chkRebootAfterScript);
			this.groupBox2.Controls.Add(this.txtLocalDUADrive);
			this.groupBox2.Controls.Add(this.label3);
			this.groupBox2.Location = new System.Drawing.Point(11, 184);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(397, 136);
			this.groupBox2.TabIndex = 8;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "Other Options";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(16, 67);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(88, 14);
			this.label2.TabIndex = 11;
			this.label2.Text = "Next DUA script";
			// 
			// txtNextScriptName
			// 
			this.txtNextScriptName.Location = new System.Drawing.Point(128, 64);
			this.txtNextScriptName.Name = "txtNextScriptName";
			this.txtNextScriptName.Size = new System.Drawing.Size(256, 20);
			this.txtNextScriptName.TabIndex = 12;
			this.txtNextScriptName.Text = "";
			// 
			// chkRebootAfterScript
			// 
			this.chkRebootAfterScript.Location = new System.Drawing.Point(16, 104);
			this.chkRebootAfterScript.Name = "chkRebootAfterScript";
			this.chkRebootAfterScript.Size = new System.Drawing.Size(208, 16);
			this.chkRebootAfterScript.TabIndex = 13;
			this.chkRebootAfterScript.Text = "Reboot at the end of the DUA script";
			// 
			// Tool_Options
			// 
			this.AcceptButton = this.btnOK;
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.CancelButton = this.btnCancel;
			this.ClientSize = new System.Drawing.Size(418, 360);
			this.Controls.Add(this.groupBox2);
			this.Controls.Add(this.groupBox1);
			this.Controls.Add(this.btnCancel);
			this.Controls.Add(this.btnOK);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.MaximizeBox = false;
			this.Name = "Tool_Options";
			this.ShowInTaskbar = false;
			this.Text = "DUA Script Generator Options";
			this.Load += new System.EventHandler(this.Tool_Options_Load);
			this.groupBox1.ResumeLayout(false);
			this.groupBox2.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion


		private void OK_Click(object sender, System.EventArgs e)
		{
			// Make sure the user has filled in all of the required
			// fields in the UI
			if ("" == txtLocationToPoll.Text)
			{
				MessageBox.Show("You must specify a location to poll", "Error - missing information", MessageBoxButtons.OK, MessageBoxIcon.Warning);
				return;
			}
			if ("" == txtLocalDUADrive.Text)
			{
				MessageBox.Show("You must specify a local DUA folder", "Error - missing information", MessageBoxButtons.OK, MessageBoxIcon.Warning);
				return;
			}
			if ( (false == btnHttpPolling.Checked) && (false == btnSharePolling.Checked) )
			{
				MessageBox.Show("You must choose a polling method", "Error - missing information", MessageBoxButtons.OK, MessageBoxIcon.Warning);
				return;
			}

			// Persist user choices to the registry
			WriteRegInformation();

			// Send an OK message and dismiss the dialog
			DialogResult=DialogResult.OK;
			this.Close();
		}

		void WriteRegInformation()
		{
			// Open the key if it exists or create it if not
            Microsoft.Win32.RegistryKey DUAScriptGen = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("SOFTWARE\\DUAScriptGen");

			// Set values in the registry based on user selections in the UI
			if (true == btnHttpPolling.Checked)
			{
				DUAScriptGen.SetValue("UseHTTPPolling", 1);
				DUAScriptGen.SetValue("DUAServerPath", txtLocationToPoll.Text);
			}
			else
			{
				DUAScriptGen.SetValue("UseHTTPPolling", 0);

				// Remove trailing backslash from the location to poll if we are using local
				// polling and not http polling
				DUAScriptGen.SetValue("DUAServerPath", txtLocationToPoll.Text.TrimEnd('\\'));
			}

			// Remove trailing backslash from the local drive
			DUAScriptGen.SetValue("LocalDrive", txtLocalDUADrive.Text.TrimEnd('\\'));
			DUAScriptGen.SetValue("NextScriptName", txtNextScriptName.Text);

			if (true == chkRebootAfterScript.Checked)
				DUAScriptGen.SetValue("RebootAfterScript", 1);
			else
				DUAScriptGen.SetValue("RebootAfterScript", 0);

		}


		void ReadRegInformation()
		{
			// Set default UI values in case the registry key has not yet
			// been created
			btnHttpPolling.Checked = true;
			btnSharePolling.Checked = false;
			txtLocationToPoll.Text = "server/share";
			txtLocalDUADrive.Text = "c:\\dua";
			txtNextScriptName.Text = "script2.dup";
			chkRebootAfterScript.Checked = true;

			// Open the key if it exists
            Microsoft.Win32.RegistryKey keyDUAScriptGen = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\\DUAScriptGen");
			if (null != keyDUAScriptGen)
			{
				// If the DUAScriptGen settings registry key does exist, make sure it has all
				// of the values we're expecting
				if ((null != keyDUAScriptGen.GetValue("UseHTTPPolling")) &&
					(null != keyDUAScriptGen.GetValue("DUAServerPath")) &&
					(null != keyDUAScriptGen.GetValue("LocalDrive")) &&
					(null != keyDUAScriptGen.GetValue("NextScriptName")) &&
					(null != keyDUAScriptGen.GetValue("RebootAfterScript")) )
				{
					// Set the polling policy radio button
					if (1 != (int)keyDUAScriptGen.GetValue("UseHTTPPolling"))
					{
						btnHttpPolling.Checked = false;
						btnSharePolling.Checked = true;
					}
					// Set the reboot checkbox
					if (1 != (int)keyDUAScriptGen.GetValue("RebootAfterScript"))
					{
						chkRebootAfterScript.Checked = false;
					}
					txtLocationToPoll.Text = (string)keyDUAScriptGen.GetValue("DUAServerPath");
					txtLocalDUADrive.Text = (string)keyDUAScriptGen.GetValue("LocalDrive");
					txtNextScriptName.Text = (string)keyDUAScriptGen.GetValue("NextScriptName");
				}
			}
		}


		private void Cancel_Click(object sender, System.EventArgs e)
		{
			DialogResult = DialogResult.Cancel;
			this.Close();
		}


		private void Tool_Options_Load(object sender, System.EventArgs e)
		{
			this.CenterToScreen();

			// Initialize UI with registry settings or default values
			ReadRegInformation();

			// Update the description for the polling location text box based on what
			// radio button for polling type is chosen
			if (true == btnHttpPolling.Checked)
				labelLocationHint.Text = "The location should be the name of a valid HTTP server and an optional relative path on that server";
			else
				labelLocationHint.Text = "The location can be a local folder on the client machine or a UNC path";
		}

		private void btnHttpPolling_CheckedChanged(object sender, System.EventArgs e)
		{
			// Update the description for the polling location text box based on what
			// radio button for polling type is chosen
			if (true == btnHttpPolling.Checked)
				labelLocationHint.Text = "The location should be the name of a valid HTTP server and an optional relative path on that server";
			else
				labelLocationHint.Text = "The location can be a local folder on the client machine or a UNC path";
		}

		private void btnSharePolling_CheckedChanged(object sender, System.EventArgs e)
		{
			// Update the description for the polling location text box based on what
			// radio button for polling type is chosen
			if (true == btnSharePolling.Checked)
				labelLocationHint.Text = "The location can be a local folder on the client machine or a UNC path";
			else
				labelLocationHint.Text = "The location should be the name of a valid HTTP server and an optional relative path on that server";
		}

	}
}
